#include <queue>
#include <nice2d/actions/sequenceaction.h>

IMPLEMENT_OBJECT(SequenceAction,Action)

class SequenceActionData
{
public:
    std::queue<ActionPointer> actions;
};

SequenceAction::SequenceAction():
    data(new SequenceActionData)
{
}

SequenceAction::~SequenceAction()
{
}

void SequenceAction::addAction(ActionPointer action)
{
    data->actions.push(action);
}

void SequenceAction::addActions(std::initializer_list<ActionPointer> list)
{
    std::for_each(list.begin(), list.end(), [&](ActionPointer action) {
        addAction(action);
    });
}

void SequenceAction::update(float deltaTime)
{
    if (!data->actions.empty()) {
        auto action = data->actions.front();
        if (action->getStatus() == ActionStatus_Idel)
            action->start();
        else if (action->getStatus() == ActionStatus_Done)
            data->actions.pop();
        else {
            action->setOwner(getOwner().lock());
            action->update(deltaTime);
        }
    }

    if (data->actions.empty())
        stop();
}
